package com.itheima.CSM_preX.aaaController;

import com.itheima.common.entity.Result;
import com.itheima.common.utils.SMSUtils;
import com.itheima.common.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/validatecode")
public class SMSController {

    @Autowired
    private SMSUtils smsUtils;
    @Autowired
    RedisTemplate redisTemplate;


    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        String code = String.valueOf(ValidateCodeUtils.generateValidateCode(4));
        smsUtils.sendMessage("SMS_476935114",telephone,code);
        String key = telephone;
        redisTemplate.opsForValue().set(key,code,10, TimeUnit.MINUTES);

        return new Result(true,"");
    }

    @PostMapping("/send4Login")
    public Result send4Login(String telephone){
        String code = String.valueOf(ValidateCodeUtils.generateValidateCode(4));
        smsUtils.sendMessage("SMS_476935114",telephone,code);
        String key = telephone;
        redisTemplate.opsForValue().set(key,code,10, TimeUnit.MINUTES);

        return new Result(true,"验证码发送成功");
    }
}
